<?php
header("content-type:text/html;charset=utf8");
class PdoClass{
    protected $pro;
    public function __construct()
    {
        $this->pro = new PDO("mysql:host=localhost;dbname=news",'root','root');
    }
    /*
     * 添加
     * */
    public function add($name,$zuo,$mean){
        $sql = "insert into ddd set name='{$name}',zuo='{$zuo}',mean='{$mean}'";
        $this->pro->exec($sql);
        return true;
    }
   /*
     * 展示
     * */
    public function show($page){
        $sql = "select * from ddd";
        $res = $this->pro->query($sql);
        $size = 3;
        $count = $res->rowCount();
        $pagenum = ceil($count/$size);
        $offset = ($page-1)*$size;
        $sql1="select * from ddd limit $offset,$size";
        $res1 = $this->pro->query($sql1);
        $data['pageinfo'] = $res1->fetchAll();
        $pagestr['prev'] = $page-1<1?1 :$page-1;
        $pagestr['next'] = $page+1>$pagenum ?$pagenum : $page+1;
        $pagestr['pagenum'] = $pagenum;
        $data['pagestr'] = $pagestr;
        return $data;
    }
    /*
     * 删除
     * */
    public function delete($id){
        $sql = "delete from ddd where id=:id";
        $res = $this->pro->prepare($sql);
        $res->execute([':id'=>$id]);
        return true;
    }
    /*
     * 查询一条数据
     * */
    public function find($id){
        $sql = "select * from ddd where id=:id";
        $res = $this->pro->prepare($sql);
        $res->execute(['id'=>$id]);
        return $res->fetch();
    }
    /*
     * 修改
     * */
    public function update($id,$name,$zuo,$mean){
        $sql = "update ddd set name='{$name}',zuo='{$zuo}',mean='{$mean}' where id=:id";
        $res = $this->pro->prepare($sql);
        $res->execute([':id'=>$id]);
        return true;
    }
    /*
     * 生成静态页
     * */
    public function state($id,$name,$zuo,$mean){
        ob_start();
        include "aa.html";
        $text = ob_get_clean();
        file_put_contents('./html/'.$id.'.html',$text);
        return true;
    }
    /*
     * 搜索
     * */
    public function sear($cha){
        $sql = "select * from ddd where name like '%$cha%'";
        $res = $this->pro->query($sql);
        $size = 3;
        $count = $res->rowCount();
        $pagenum = ceil($count/$size);
        $page = isset($_GET['page'])? $_GET['page']:'1';
        $offset = ($page-1)*$size;
        $sql1="select * from ddd where name like'%$cha%' limit $offset,$size";
        $res1 = $this->pro->query($sql1);
        $data['pageinfo'] = $res1->fetchAll();
        $pagestr['prev'] = $page-1<1?1 : $page-1;
        $pagestr['next'] = $page+1>$pagenum ?$pagenum : $page+1;
        $pagestr['pagenum'] = $pagenum;
        $data['pagestr'] = $pagestr;
        return $data;

    }


}





?>